package com.woniuxy.woniuticket.movieinfomodule.controller;

import com.woniuxy.woniuticket.movieinfomodule.service.CinemaInfoService;
import com.woniuxy.woniuticket.movieinfomodule.service.ManagementPackageService;
import com.woniuxy.woniuticket.moviemodule.service.AdminService;
import com.woniuxy.woniuticket.pojo.CinemaInfo;
import com.woniuxy.woniuticket.pojo.CinemaLogin;
import com.woniuxy.woniuticket.pojo.ManagementPackage;
import com.woniuxy.woniuticket.resultcommon.JsonResult;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.security.provider.MD5;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 赵晓东 on 2019/6/17/017.
 */
@Controller
@RequestMapping("/cinemaInfo")
public class CinemaInfoController {
    @Resource
    private CinemaInfoService cinemaInfoService;
    @Resource
    private ManagementPackageService managementPackageService;
    @Resource
    private AdminService adminService;

    @RequestMapping("/select")
    @ResponseBody
    public JsonResult selectCinema(){
        List<CinemaInfo> cinemaInfos = cinemaInfoService.selectCinemaInfo();
        return JsonResult.ok("查询成功",cinemaInfos.get(0));
    }

    @RequestMapping("/update")
    public String updateCinema(CinemaInfo cinemaInfo){
        cinemaInfoService.updateCinemaInfo(cinemaInfo);
        return "/moviemanager/pages/info/cinemaInfo";
    }

    @RequestMapping("/updatePackage")
    @ResponseBody
    public JsonResult updatePackage(String str){
        String[] strs = str.split(",");
        String name = strs[0];
        String pwd = strs[1];
        Md5Hash md5Hash = new Md5Hash(pwd, name, 2);
        CinemaLogin login = adminService.login(name);
        int row = 0;
        if (login!=null){
            if (md5Hash.toString().equals(login.getPassword())){
                ManagementPackage managementPackage = new ManagementPackage();
                managementPackage.setPackagename(strs[2]);
                row = managementPackageService.updatePackage(managementPackage);
            }
        }
        if (row > 0) {
            return JsonResult.ok("更改套餐成功！");
        }
        return JsonResult.error();
    }
}
